package com.kgc.warehouse.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kgc.warehouse.system.common.CodeMsg;
import com.kgc.warehouse.system.common.ResponseResult;
import com.kgc.warehouse.system.dto.PwdDto;
import com.kgc.warehouse.system.dto.UserDto;
import com.kgc.warehouse.system.dto.UserPageDto;
import com.kgc.warehouse.system.entity.User;
import com.kgc.warehouse.system.service.UserService;
import com.kgc.warehouse.system.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("user")
@Slf4j
public class  UserController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private UserService userService;

    @RequestMapping("login")
    public ResponseResult login(UserDto userDto, HttpSession session, Model model) {
        String redisCode = redisTemplate.opsForValue().get(session.getId());

        if (redisCode == null) {
            return new ResponseResult(CodeMsg.CAPTCHA_EXPIRE);
        } else {
            if (redisCode.equals(userDto.getCaptcha())) {
                Subject subject = SecurityUtils.getSubject();
                UsernamePasswordToken token = new UsernamePasswordToken(userDto.getLoginname(), userDto.getPwd());

                if (userDto.getRememberMe() != null) {
                    token.setRememberMe(true);
                }
                String loginname = userDto.getLoginname();
                LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
                userLambdaQueryWrapper.eq(User::getLoginname,loginname);
                User one = userService.getOne(userLambdaQueryWrapper);
                subject.login(token);
                session.setAttribute("user",one);
                //model.addAttribute("username",one.getName());
                //log.info(String.valueOf(model.getAttribute("username")));
                return new ResponseResult(CodeMsg.SUCCESS);

            } else {
                return new ResponseResult(CodeMsg.CAPTCHA_ERROR);
            }
        }
    }

    //退出登录
    @RequestMapping("logOut")
    public ResponseResult logout() {

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResponseResult(CodeMsg.SUCCESS);
    }

    //分页查询
    @RequestMapping("loadAllUser")
    public ResponseResult loadAllUser(UserPageDto userPageDto) {
        System.out.println(userPageDto.getLimit());
        System.out.println("userPageDto.getPage() = " + userPageDto.getPage());
        //======

        return userService.selectPage(userPageDto);
    }

    //
    @RequestMapping("mgrSelect")
    public ResponseResult mgrSelect() {
        List<User> list = userService.list();
        return new ResponseResult(list, null, CodeMsg.SUCCESS);
    }

    //添加用户
    @RequestMapping("add")
    @RequiresPermissions("user:create")
    public ResponseResult add(@RequestBody UserVo userVo) {
        return userService.addUser(userVo);
    }

    //编辑回显数据
    @RequestMapping("getById")
    public ResponseResult getById(Integer id, Model model) {
        log.info("======" + String.valueOf(id));

        User byId = userService.getById(id);
        model.addAttribute("byId",byId);
        return userService.getUserById(id);
    }

    //删除用户
//    @RequiresRoles({""})
    @RequestMapping("delete")
    @RequiresPermissions("user:delete")
    public ResponseResult delete(String ids) {

        return userService.deletebyId(ids);
    }

    @RequestMapping("update")
    @RequiresPermissions("user:update")
    public ResponseResult update(@RequestBody UserVo userVo)
    {
        return userService.updateUser(userVo);
    }

    @RequestMapping("loadMenu")
    public Map<String, Object> menu(String username) {
        return userService.menu(username);
    }

    @RequestMapping("updateAvailable")
   public ResponseResult updateAvailable(Integer id,Integer available)
    {
        log.info(id+"==="+available);
        return userService.updateAvailable(id,available);
    }

    @RequestMapping("resetPwd")
    @RequiresPermissions("user:resetPwd")
    public ResponseResult resetPwd(Integer id)
    {
        return userService.resetPwd(id);
    }

    @RequestMapping("changePwd")
    public ResponseResult changePwd(@RequestBody PwdDto pwdDto,HttpSession session)
    {
        User user = (User) session.getAttribute("user");
        Integer a=userService.changePwd(user,pwdDto);
        if (a==1)
        {
            return new ResponseResult(CodeMsg.OLEPWD_ERRO);
        }else if (a==2)
        {
            return new ResponseResult(CodeMsg.AGAINNEW_ERRO);
        }

        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ResponseResult(CodeMsg.UPDATE_SUCCESS);
    }
}
